define([ "app" ], function(app) {

	var deps = [ "$http" ];

	var museService = function($http) {
		return {
			postObj : function(url, params) {
				return $http.post(url, angular.toJson(params));
			},
			post : function(url, params) {
				return $http.post(url, params, {
					headers : {
						"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"
					}
				});
			},
			query : function(url, params) {
				var query = "";
				if (params != null) {
					query = "?";
					var arr = new Array();
					for ( var key in params) {
						arr.push(key + "=" + params[key]);
					}
					query += arr.join("&");
				}
				return $http.get(url + query);
			},
			get : function(url, params) {
				return $http.get(url, params, {
					headers : {
						"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"
					}
				});
			},
			alert : function(msg) {
				alert(msg);
			},
			confirm : function(msg) {
				return confirm(msg);
			},

			// 取map中的key值
			getMapKeyArray : function(map) {
				var array = [];
				for ( var i in map) {
					array.push(i);
				}
				return array;
			},

			// 取map中的value值
			getMapValueArray : function(map) {
				var array = [];
				for ( var i in map) {
					array.push(map[i]);
				}
				return array;
			},

			// 取第一个被选中的对象
			getFirstItem : function(allobj, selobj) {
				var rst = "";
				for (var i = 0; i < allobj.length; i++) {
					// 这2种方法都可("key" in obj) obj.hasOwnProperty("key")
					if (selobj.hasOwnProperty(allobj[i].id)) {
						return allobj[i].id;
					}
				}
				return rst;
			},

			// 全选
			toggleAll : function(objList, selObjList, isSelectAll) {
				var rst = {};
				if (isSelectAll) {// 取消
					selObjList = {};
					isSelectAll = false;
				} else {// 选中
					for ( var i in objList) {
						selObjList[objList[i].id] = objList[i];
					}
					isSelectAll = true;
				}
				rst.selObjList = selObjList;
				rst.isSelectAll = isSelectAll;
				return rst;
			},

			// 单选
			toggleSingle : function(selObjList, obj) {
				if (selObjList[obj.id]) {
					delete selObjList[obj.id];
				} else {
					selObjList[obj.id] = obj;
				}
				return selObjList;
			},

			// 选择复选框，指定名
			toggleCheckBox : function(selObjList, obj) {
				if (selObjList[obj]) {
					delete selObjList[obj];
				} else {
					selObjList[obj] = obj;
				}
				return selObjList;
			},

			// 计算firstPageSize,lastPageSize（从第几条到第几条）
			calFirstAndLastPage : function(pageNo, pageSize, totalItems) {
				var rst = {};
				var firstPageSize = (pageNo - 1) * pageSize + 1;
				var lastPageSize = pageNo * pageSize;
				if (totalItems < lastPageSize) {
					lastPageSize = totalItems;
				}
				rst.firstPageSize = firstPageSize;
				rst.lastPageSize = lastPageSize;
				return rst;
			}

		};
	};

	museService.$inject = deps;
	return app.lazy.service("MuseService", museService);
});
